﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
public partial class admin_News_Edit : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        AdminCs.CheckLoginState();
        if (!this.IsPostBack)
        {
            if (Request.QueryString["n"] == "edit")　//显示文本信息
            {
                News_Txt();
            }
            else if (Request.QueryString["n"] == "del")// 删除新闻
            {
                News_del();
            }
        }
    }
    protected void UpImg_Click(object sender, EventArgs e)//上传图片
    {
        if (FileUpload1.PostedFile.ContentLength < 500000)//100000为100K
        {

            string fileFullname = this.FileUpload1.FileName;
            string dataName = DateTime.Now.ToString("yyyyMMddhhmmss");
            string fileName = fileFullname.Substring(fileFullname.LastIndexOf("\\") + 1);
            string type = fileFullname.Substring(fileFullname.LastIndexOf(".") + 1);
            if (type == "bmp" || type == "jpg" || type == "gif" || type == "JPG" || type == "BMP" || type == "GIF")
            {
                this.FileUpload1.SaveAs(Server.MapPath("../Images") + "\\" + dataName + "." + type);
                string ProImg = "Images/" + dataName + "." + type;
                this.txtProImg.Text = "Images/" + dataName + "." + type;
            }
            else
            {
                Response.Write("<script language='javascript'>alert('支持格式：|jpg|gif|bmp|');</script>");
            }
        }
        else
        {
            Response.Write("<script language='javascript'>alert('你的图片已经超过500K的大小！');</script>");
        }
    }
    private void News_Txt()//显示文本分类
    {
        int id = int.Parse(Request.QueryString["n_ID"]);
        DataTable dt = Class1.ExecSel("select * from ZhanHui where n_ID='" + id + "'");
        TextBox1.Text = Convert.ToString(dt.Rows[0]["n_Title"]);
        TextBox2.Text = Convert.ToString(dt.Rows[0]["n_zhtime"]);
        TextBox3.Text = Convert.ToString(dt.Rows[0]["n_zhaddress"]);
        TextBox4.Text = Convert.ToString(dt.Rows[0]["n_Source"]);
        txtProImg.Text = Convert.ToString(dt.Rows[0]["n_Img"]);
        ftbContent.Text = Convert.ToString(dt.Rows[0]["n_Content"].ToString());

        if (dt.Rows[0]["n_Lock"].ToString() == "1")
        {
            CheckBox1.Checked = true;
        }
        else
        {
            CheckBox1.Checked = false;
        }
        //if (dt.Rows[0]["n_HomeShow"].ToString() == "1")
        //{
        //    CheckBox2.Checked = true;
        //}
        //else
        //{
        //    CheckBox2.Checked = false;
        //}

        if (dt.Rows[0]["n_Class"].ToString().Trim() == "国内展会")
        {
            this.DropDownList1.Items[0].Selected = false;
            this.DropDownList1.Items[1].Selected = true;
            this.DropDownList1.Items[2].Selected = false;
            this.DropDownList1.Items[3].Selected = false;
            this.DropDownList1.Items[4].Selected = false;
        }
        else if (dt.Rows[0]["n_Class"].ToString().Trim() == "国外展会")
        {
            this.DropDownList1.Items[0].Selected = false;
            this.DropDownList1.Items[1].Selected = false;
            this.DropDownList1.Items[2].Selected = true;
            this.DropDownList1.Items[3].Selected = false;
            this.DropDownList1.Items[4].Selected = false;
        }
        else if (dt.Rows[0]["n_Class"].ToString().Trim() == "展会常识")
        {
            this.DropDownList1.Items[0].Selected = false;
            this.DropDownList1.Items[1].Selected = false;
            this.DropDownList1.Items[2].Selected = false;
            this.DropDownList1.Items[3].Selected = true;
            this.DropDownList1.Items[4].Selected = false;
        }
        else if (dt.Rows[0]["n_Class"].ToString().Trim() == "展馆介绍")
        {
            this.DropDownList1.Items[0].Selected = false;
            this.DropDownList1.Items[1].Selected = false;
            this.DropDownList1.Items[2].Selected = false;
            this.DropDownList1.Items[3].Selected = false;
            this.DropDownList1.Items[4].Selected = true;
        }
    }
    private void News_Add()　//添加新闻
    {
        string S_Lock;
        string Home_Show;
        int n_Hits = 0;
        string Dr = DropDownList1.SelectedItem.Text;
        string Title = TextBox1.Text;
        string Zhtime = TextBox2.Text;
        string Zhaddress = TextBox3.Text;
        string Source = TextBox4.Text;
        string Img = txtProImg.Text;
        string Content = ftbContent.Text.Trim();
        DateTime Time = DateTime.Now;

        if (this.CheckBox1.Checked)
        {
            S_Lock = "1";

        }
        else
        {

            S_Lock = "0";
        }
        Home_Show = "1";
        //if (this.CheckBox2.Checked)
        //{
        //    Home_Show = "1";

        //}
        //else
        //{

        //    Home_Show = "0";
        //}

        if (this.DropDownList1.SelectedItem.Text == "请选择")
        {
            Class1.AlertShow("请选择分类！");
            return;
        }
        else if (Title == "")
        {
            Class1.AlertShow("标题不能为空！");
            return;
        }
        else if(Title.Length>100)
        {
            Class1.AlertShow("标题不能大于100个字！");
            return;
        }
        string sql = "insert into ZhanHui(n_Class,n_Title,n_zhtime,n_zhaddress,n_Source,n_Img,n_Content,n_Lock,n_HomeShow,n_Time,n_Hits)values('" + Dr + "','" + Title + "','" + Zhtime + "','" + Zhaddress + "','" + Source + "','" + Img + "','" + Content + "'," + S_Lock + "," + Home_Show + ",'" + Time + "'," + n_Hits + ")";
        Class1.ExecSql(sql);
        Class1.AlertShow("添加成功！", "Show_Edit.aspx?n=add");
    }
    private void News_Edit()　//修改新闻
    {
        int id = int.Parse(Request.QueryString["n_ID"]);
        string S_Lock;
        string Home_Show;
        string Dr = DropDownList1.SelectedItem.Text;
        string Title = TextBox1.Text;
        string Zhtime = TextBox2.Text;
        string Zhaddress = TextBox3.Text;
        string Source = TextBox4.Text;
        string Img = txtProImg.Text;
        string Content = ftbContent.Text.Trim();
        if (this.CheckBox1.Checked)
        {
            S_Lock = "1";

        }
        else
        {

            S_Lock = "0";
        }
        Home_Show = "1";
        //if (this.CheckBox2.Checked)
        //{
        //    Home_Show = "1";

        //}
        //else
        //{

        //    Home_Show = "0";
        //}

        if (this.DropDownList1.SelectedItem.Text == "请选择")
        {
            Class1.AlertShow("请选择分类！");
            return;
        }
        else if (Title == "")
        {
            Class1.AlertShow("标题不能为空！");
            return;
        }
        else if (Title.Length > 100)
        {
            Class1.AlertShow("标题不能大于100个字！");
            return;
        }
        string sql = ("update ZhanHui set n_Class='" + Dr + "',n_Title='" + Title + "',n_zhtime='" + Zhtime + "',n_zhaddress='" + Zhaddress + "',n_Source='" + Source + "',n_Img='" + Img + "',n_Content='" + Content + "',n_Lock='" + S_Lock + "',n_HomeShow='" + Home_Show + "' where n_ID='" + id + "'");
        Class1.ExecSql(sql);
        Class1.AlertShow("修改成功！", "Show_Manage.aspx?zh_Class=");
    }
    private void News_del()//删除新闻
    {
        int id = int.Parse(Request.QueryString["n_ID"]);
        string sql = "delete ZhanHui where n_ID=" + id;
        Class1.ExecSql(sql);
        Class1.AlertShow("删除成功！", "Show_Manage.aspx?zh_Class=");
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        if (Request.QueryString["n"] == "add")
        {
            News_Add();
        }
        else if (Request.QueryString["n"] == "edit")
        {
            News_Edit();
        }
    }
    #region 选择时间
    protected void imgCalendar_Click(object sender, ImageClickEventArgs e)
    {
        calendar.Visible = true;

    }

    protected void calendar_SelectionChanged(object sender, EventArgs e)
    {
        TextBox2.Text = calendar.SelectedDate.ToString("yyyy-MM-dd");
        calendar.Visible = false;

    }


    #endregion
}
